package com.xyh.springboot.controller;

import com.xyh.springboot.model.User;
import com.xyh.springboot.service.IUserService;
import com.xyh.springboot.util.ShiroPasswordSaltUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.amqp.RabbitRetryTemplateCustomizer;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @Author:kyle
 * @Date:2021/1/4 - 10:22
 * @Project:springboot
 * @Package:com.xyh.springboot.controller
 * @Version:1.0
 */
@Controller
@RequestMapping("/user")
public class UserController2 {


    @Autowired
    private IUserService userService;

    //跳转到login界面
    @RequestMapping("/toLogin")
    public String toLogin(){

        return "login";
    }

    //跳转到register界面
    @RequestMapping("/toRegister")
    public String toRegister(@ModelAttribute("user") User user){

        return "register";
    }

    //跳转到index界面
    @RequestMapping("/toIndex")
    public String toIndex(){
        return "index";
    }

    //跳转到noAuth界面
    @RequestMapping("/toNoAuth")
    public String toNoAuth(){
        return "noAuth";
    }

    @RequestMapping("/logout")
    public String logout(){

        SecurityUtils.getSubject().logout();

        return "index";
    }

    //新增界面
    @RequestMapping("/add")
//    @RequiresPermissions("user:add")
    public String add(User user){
        //创建盐
        String salt = ShiroPasswordSaltUtil.createSalt();
        user.setSalt(salt);

        //设置加盐加密后的密码
        user.setPassword(ShiroPasswordSaltUtil.md5Salt(user.getPassword(),salt));

        boolean flag = userService.save(user);
        if (flag) {

            return "index";
        }else {
            return "login";
        }

    }

    //更新界面
    @RequestMapping("update")
//    @RequiresPermissions("user:update")
    public String update(){

        return "/user/update";
    }

    //删除界面
    @RequestMapping("delete")
    public String delete(){

        return "/user/delete";
    }


    //用户登录
    @PostMapping("/login")
    public String login(String username, String password, ModelMap modelMap){
        //通过shiro获取subject 实体对象--这里是用户
        Subject subject = SecurityUtils.getSubject();
        //封装用户数据
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);

        //执行登录方法
        //只需要执行login方法，就会自动去执行UserRealm中的认证逻辑
        try {
            subject.login(token);
            return "index";
        }catch (UnknownAccountException e){
            modelMap.addAttribute("msg","账户不存在");
           e.printStackTrace();
           return "error";
        }catch (IncorrectCredentialsException e){
            modelMap.addAttribute("msg","密码错误");
            e.printStackTrace();
            return "error";
        }

    }
}
